package com.ccl.iot.views;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.ccl.iot.R;
import com.ccl.iot.batch.IOTBatchInterface;

public class CheckableCustomIrView extends MarqueeLabel implements IOTBatchInterface.IOTBlirViewCheckInterface {

    private boolean mCheckable;
    private boolean mChecked;
    private Bitmap mChectedBitmap;
    private Bitmap mUnchectedBitmap;
    private Paint mPaint;

    private IOTBatchInterface.OnBlirViewChecked mOnBlirViewChected = null;

    public CheckableCustomIrView(Context context) {
        this(context, null);
    }

    public CheckableCustomIrView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }

    public void setOnBlirViewChected(IOTBatchInterface.OnBlirViewChecked onBlirViewChected) {
        mOnBlirViewChected = onBlirViewChected;
    }

    @Override
    public void setCheckable(boolean checkable) {
        if (mCheckable != checkable) {
            if (checkable) {
                if (mChectedBitmap == null) {
                    mChectedBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.choice);
                }

                if (mUnchectedBitmap == null) {
                    mUnchectedBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.smart_unchoice);
                }
            }
            mCheckable = checkable;
            invalidate();
        }
    }

    @Override
    public void setChecked(boolean checked) {
        if (mChecked != checked) {
            mChecked = checked;
            invalidate();
            onChecked(mChecked);
        }
    }

    @Override
    public void onChecked(boolean checked) {
        if (mOnBlirViewChected != null) {
            mOnBlirViewChected.onChected(checked, (String) getTag(), this);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mCheckable && getTag() != null) {
            if (mChecked) {
                canvas.drawBitmap(mChectedBitmap, getWidth() - mChectedBitmap.getWidth(), 0, mPaint);
            } else {
                canvas.drawBitmap(mUnchectedBitmap, getWidth() - mUnchectedBitmap.getWidth(), 0, mPaint);
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (this.isSelected()) {
            return super.onTouchEvent(event);
        }
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (mCheckable && getTag() != null && event.getX() >= getWidth() - mUnchectedBitmap.getWidth() && event.getY() <= mUnchectedBitmap.getHeight()) {
                    return true;
                }
                break;
            case MotionEvent.ACTION_MOVE:

                break;
            case MotionEvent.ACTION_UP:
                if (mCheckable && getTag() != null && event.getX() >= getWidth() - mUnchectedBitmap.getWidth() && event.getY() <= mUnchectedBitmap.getHeight()) {
                    mChecked = !mChecked;
                    onChecked(mChecked);
                    invalidate();
                    return true;
                }
                break;
        }
        return super.onTouchEvent(event);
    }
}
